package com.cjb.nettywebsocket.task;

import com.cjb.common.common.cache.RedisCache;
import com.cjb.common.util.Common;
import com.cjb.common.util.JsonUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * All rights Reserved, Designed By  chenjinbin
 *
 * @author chenjinbin
 * @Project: cloud-netty-websocket
 * @Package com.cjb.nettywebsocket.task
 * @title WebSocketTask
 * @Description
 * @email
 * @date 2023-11-29 14:54
 * @since JDK 1.8
 */
@Component
@Slf4j
public class WebSocketTask implements Runnable{

    @Autowired
    private RedisCache redisCache;

    private ChannelHandlerContext ctx;
    private String message;


    public void setCtx(ChannelHandlerContext ctx) {
        this.ctx = ctx;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public void run() {
//        System.out.println("启动一个线程");
        String jsonMsg = (String)redisCache.hGet("message","deviceStatus");
        if(Common.isEmpty(jsonMsg)){

        }
        jsonMsg =jsonMsg == "信息获取失败" ? "" :jsonMsg;
        log.info("打印设备发送信息{}", JsonUtils.toJsonString(jsonMsg));
        log.info(Thread.currentThread().getName() + "服务器时间" + LocalDateTime.now() + "cjb");
        ctx.channel().writeAndFlush(new TextWebSocketFrame(jsonMsg));
    }

}
